package com.anlogic.sdk.ui.tests.uc6.projectattr;

import static org.junit.Assert.*;

import java.io.File;
import java.util.Iterator;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.anlogic.sdk.env.utils.EnvCheckUtils;
import com.anlogic.sdk.ui.tests.utils.WorkspaceUtils;
import com.anlogic.sdk.utils.EnvUtils;
import com.anlogic.sdk.utils.UIUtils;
import com.anlogic.sdk.workspace.FDWorkspace;

public class UC6_TC0013_Test {

	private static SWTWorkbenchBot bot;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {

		bot = new SWTWorkbenchBot();
		SWTBotPreferences.TIMEOUT = 10000;
		SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"), 90 * 1000);
		 SWTBotPreferences.PLAYBACK_DELAY = 10;

	}

	@Before
	public void setUp() throws Exception {
		WorkspaceUtils.resetworkbench(bot);
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {

	}

	@After
	public void tearDown() throws Exception {

		WorkspaceUtils.cleanWorkspace();
	}

	@Test
	public void buildTest() throws Exception {
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"), 60 * 1000);

		bot.waitUntil(new DefaultCondition() {
			@Override
			public boolean test() throws Exception {
				// 检查菜单是否可用
				return bot.menu("File").menu("New").menu("Project...").isEnabled();
			}

			@Override
			public String getFailureMessage() {
				return "Menu 'Platform Project' is not available or not enabled.";
			}
		}, 60 * 1000);
		bot.menu("File").menu("New").menu("Project...").click();
		bot.tree().getTreeItem("C/C++").doubleClick();

		bot.tree().getTreeItem("C/C++").getNode("C/C++ Project").select();
		bot.tree().getTreeItem("C/C++").getNode("C/C++ Project").doubleClick();
		bot.table().select(0);
		bot.table().getTableItem(0).doubleClick();
		bot.textWithLabel("&Project name:").setText("riscv");
		bot.tree().getTreeItem("Executable").getNode("RISCV C Project (SF1)").select();
		bot.button("Next >").click();
		bot.button("Finish").click();
		bot.viewByTitle("Project Explorer").bot().tree().select("riscv").contextMenu("Reset Project").click();
		bot.comboBox().setSelection("sf1");
		bot.button("Reset").click();
		bot.button("Close").click();
		bot.tree().getTreeItem("riscv").select();
		
		bot.tree().getTreeItem("riscv").doubleClick();

		bot.viewByTitle("Project Explorer").show();
		SWTBotTreeItem item = bot.viewByTitle("Project Explorer").bot().tree().getTreeItem("riscv");

		// 展开、确保 item 可见（可选）
		item.select();

		bot.activeShell().pressShortcut(SWT.ALT, SWT.CR); // Alt + Enter 打开属性对话框

		
		SWTBotShell shell = bot.shell("Properties for riscv");
		shell.activate();

		SWTBotTree tree = shell.bot().tree(0); // 或者 tree(1)，根据实际尝试

		// 先列出所有项，方便排查
		for (SWTBotTreeItem item1 : tree.getAllItems()) {
		    System.out.println("Tree item1: [" + item1.getText() + "]");
		}
		bot.waitUntil(new DefaultCondition() {
			@Override
			public boolean test() throws Exception {
				// 检查菜单是否可用
				return tree.isEnabled();
			}

			@Override
			public String getFailureMessage() {
				return "not available or not enabled.";
			}
		}, 60 * 1000);
		

		bot.waitUntil(new DefaultCondition() {
		    @Override
		    public boolean test() {
		        try {
		            return tree.getTreeItem("C/C++ Build") != null;
		        } catch (WidgetNotFoundException e) {
		            return false;
		        }
		    }

		    @Override
		    public String getFailureMessage() {
		        return "Tree item 'C/C++ Build' 一直未出现";
		    }
		}, 30 * 1000);  
		tree.getTreeItem("C/C++ Build").select();
		tree.getTreeItem("C/C++ Build").expand();
		tree.getTreeItem("C/C++ Build").getNode("Settings").select();
		bot.cTabItem("Toolchains").activate();
		
		bot.comboBox(1).setSelection("RISC-V Anlogic GCC (riscv32-anlogic-elf-gcc)");
		bot.button("Apply").click();
		bot.button("Apply and Close").click();
		//bot.tree(1).getTreeItem(process).select();

		// bot.sleep(1000); // 等右侧 UI 加载

		//bot.comboBox(index).setSelection(Selection);

	}
	
}
